package com.ly.task;

import cn.hutool.core.date.DateUtil;
import com.ly.entity.CarOrder;
import com.ly.service.CarOrderService;
import com.ly.utils.DateUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
public class SpringTaskTest {

    @Autowired
    private CarOrderService carOrderService;

//    @Scheduled(cron  = "*/10 * * * * *")
    @Scheduled(cron = "0 0 10,14,16 * * ?")
    public void carOrderTask() throws InterruptedException {

        System.out.println("订单逾期任务开始执行");

        List<CarOrder> carOrders = carOrderService.findByOverdue();

        if(carOrders!=null){
            carOrders.stream().forEach(o->{
                try {
                    o.setOrderStatus(3);
                    Double avgPrice = o.getPrice()/o.getLoanableTime();
                    o.setPenalSum((DateUtils.getDaysNum(o.getReturnTime())*avgPrice)*2); //违约金计算 超过一天按双倍计算

                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            carOrderService.updateCarOrderList(carOrders);
        }


        System.out.println("订单逾期任务执行完毕");
    }


}
